﻿using LPBToolsLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 电子票存档工具
{
    public partial class FDBSerConfig : Form
    {
        SQLSerConfig DBServerCFG { get; }
        public FDBSerConfig(SQLSerConfig DestDBServerCFG)
        {
            InitializeComponent();
            DBServerCFG = DestDBServerCFG;
            if (DBServerCFG != null)
            {
                textBoxSerAddress.Text = DBServerCFG.Address;
                textBoxDBUser.Text = DBServerCFG.DBUser;
                textBoxDBPass.Text = DBServerCFG.DBPass;
                textBoxDBName.Text = DBServerCFG.DBName;
                buttonSave.Enabled = false;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (checkDBINfo())
            {
                DBServerCFG.Address = textBoxSerAddress.Text;
                DBServerCFG.DBUser = textBoxDBUser.Text;
                DBServerCFG.DBPass = textBoxDBPass.Text;
                DBServerCFG.DBName = textBoxDBName.Text;
                MessageBox.Show("保存成功！");
                Close();
            }
            else
            {
                MessageBox.Show("输入信息无法成功连接数据库！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public bool checkDBINfo()
        {
            if (BaseTools.PingTest(textBoxSerAddress.Text))
            {
                string sConnectStr = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}";// ;TrustServerCertificate = true; MultipleActiveResultSets=true; 
                sConnectStr = string.Format(sConnectStr,
                    textBoxSerAddress.Text,
                    textBoxDBName.Text,
                    textBoxDBUser.Text,
                    textBoxDBPass.Text);
                try
                {
                    using (SqlConnection tDBCon = new SqlConnection(sConnectStr))
                        try
                        {
                            tDBCon.Open();
                            SqlCommand SelectCommand = new SqlCommand("select count(*) from sysobjects", tDBCon);
                            SelectCommand.ExecuteScalar();
                            return true;
                        }
                        catch
                        {

                        }
                        finally
                        {
                            tDBCon.Close();
                        }
                }
                catch
                {
                }
            }
            return false;
        }

        private void textBoxSerAddress_TextChanged(object sender, EventArgs e)
        {
            buttonSave.Enabled = true;
        }
    }

    public class AppRunConfig : SQLSerConfig
    {
        public AppRunConfig(JSONINISettings tBaseSetCFG, string DestSegment = "") : base(tBaseSetCFG, DestSegment)
        {
        }

        [DisplayName("上次登录用户名")]
        public string lastLoginUser
        {
            get
            {
                if (BaseSetCFG != null)
                    return BaseSetCFG.getCFGValue<string>(Segment, "LastLoginUser", "");
                return "";
            }
            set { BaseSetCFG?.setCFGValue(Segment, "LastLoginUser", value); }
        }

        [DisplayName("上次登录用密码")]
        public string lastLoginPass
        {
            get
            {
                string sTmp = "";
                if (BaseSetCFG != null)
                {
                    sTmp = BaseSetCFG.getCFGValue<string>(Segment, "lastLoginPass", "");
                    if (!string.IsNullOrEmpty(sTmp))
                    {
                        try
                        {
                            sTmp = SecurityTools.AESSelfDecrypt(sTmp);
                        }
                        catch
                        {
                            return "";
                        }
                    }
                }
                return sTmp;
            }
            set { BaseSetCFG?.setCFGValue(Segment, "lastLoginPass", SecurityTools.AESSelfEncrypt(value)); }
        }
    }
}
